package game.manager
{
	import flash.utils.Dictionary;
	
	/**
	 * 可能会有一些解析的数据
	 */ 
	public class XMLManager
	{
		private static var _instance:XMLManager;
		
		private var _items:XML;
		
		public function set items(value:XML):void {
			this._items = value;
		}
		
		public function get items():XML {
			if(this._items == null){
				throw new Error("not loaded completed xml !");
			}
			return this._items;
		}
		
		public function XMLManager()
		{
			_instance = this;
		}
		
		public static function get instance():XMLManager {
			if(_instance == null){
				_instance = new XMLManager();
			}
			return _instance;
		}
		
		/**
		 * 这里暂时不需要做缓存
		 * <item Name="abc" test= "abbb" />
		 * return obj.Name = "abc"
		 * 		  obj.test = "abbb"
		 */ 
		private function xml2Object(xml:XML):Object {
			var result:Object = {};
			for each(var attribute:* in xml.attributes()){
				result[attribute.name().toString()] = attribute.toString();
			}
			return result;
		}
		
		private static const XML_CACHE:String = "XML_CACHE";
		private static const P_CACHE_FLAG:String = "P_CACHE";
		private var xmlCache:Dictionary = new Dictionary(true);
		
		/**
		 * 根据sku或者Name获取指定的值
		 * 把相应的循环做出来了
		 */ 
		private var P_CACHE:Dictionary = new Dictionary(true);
		public function getPropertyByName(node:String,subNode:String = null,name:String = null,property:String = "className"):Object {
			var result:Object;
			var cacheKey:String = node + subNode + name + property + P_CACHE_FLAG;
			if(P_CACHE[cacheKey]){
				return P_CACHE[cacheKey];
			}
			var items:Object = getProperties(node,subNode);
			for each(var item:Object in items){
				if(item[property] === name){
					result = item;
					break;
				}
			}
			return result;
		}
		
		/**
		 * 这个方法需要调整一下
		 */ 
		public function getProperties(node:String,subNode:String = null):Array {
			var result:Array = [];
			var cacheKey:String = node + subNode + XML_CACHE;
			if(xmlCache[cacheKey]){
				return xmlCache[cacheKey];
			}
			for(var i:int = 0; i < this.items.children().length(); i++){
				var xml:XML = this.items.children()[i];
				var nodeName:String = xml.localName();
				if(nodeName == node){
					for(var j:int = 0; j < xml.children().length(); j++){
						var temp:XML = xml.children()[j];
						var obj:Object;
						if(subNode != null){
							var localeName:String = temp.localName().toString();
							if(localeName == subNode){
								for each(var element:XML in temp.elements()){
									obj = xml2Object(element);
									result.push(obj);
								}
							}
						}else{
							obj = xml2Object(temp);
							result.push(obj);
						}
					}
				}
			}
			xmlCache[cacheKey] = result;
			return result;
		}
		
		public function getConfig(node:String,subNode:String = null,property:String = "className"):* {
			var result:*;
			var cacheKey:String = node + subNode + property + P_CACHE_FLAG;
			if(P_CACHE[cacheKey]){
				return P_CACHE[cacheKey];
			}
			var items:Object = getProperties(node,subNode);
			for each(var item:Object in items){
				for(var key:String in item){
					if(key == property){
						result = item[key];
						break;
					}
				}
			}
			return result;
		}
	}
}